package top.scsoul.pas.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import top.scsoul.pas.bean.XhrResponse;
import top.scsoul.pas.entity.Clazz;
import top.scsoul.pas.entity.Student;
import top.scsoul.pas.service.ClazzService;
import top.scsoul.pas.service.StudentService;

import java.util.List;

@Slf4j
@Controller
public class StudentController {
    @Autowired
    StudentService studentService;
    @Autowired
    ClazzService clazzService;

    @PreAuthorize("hasAnyRole('ADMIN')")
    @RequestMapping("student/list")
    public String studentList(
            @RequestParam(required = false) int currentPage,
            @RequestParam(required = false) int pageSize,
            Model model) {
        if (currentPage <= 0) {
            currentPage = 1;
        }
        Page<Student> studentPage = studentService.listStudent(currentPage, pageSize);
        model.addAttribute("studentPage", studentPage);
        return "student_list";
    }

    @RequestMapping("student/add")
    public String addUserPage() {
        return "student_add";
    }
    @PreAuthorize("hasAnyRole('ADMIN')")
    @ResponseBody
    @RequestMapping("api/addStudent")
    public XhrResponse addStudent(Student student, Long clazzId) {
        Clazz clazz = clazzService.getById(clazzId);
        student.setClazz(clazz);
        studentService.insert(student);
        return XhrResponse.success();
    }

    @PreAuthorize("hasAnyRole('ADMIN')")
    @ResponseBody
    @RequestMapping("api/deleteStudent")
    public XhrResponse deleteStudent(Long id) {
        try {
            studentService.deleteById(id);
            return XhrResponse.success();

        } catch (Exception e) {
            return XhrResponse.error("");
        }
    }

    @RequestMapping("student/edit/{id}")
    public String editStudentPage(@PathVariable Long id, Model model) {
        Student student = studentService.queryById(id);
        model.addAttribute("student", student);
        return "student_edit";
    }

    @ResponseBody
    @RequestMapping("api/updateStudent")
    public XhrResponse updateStudent(Student student, Long clazzId) {
        Clazz clazz = clazzService.getById(clazzId);
        student.setClazz(clazz);
        try {
            studentService.update(student);
        } catch (Exception e) {
            return XhrResponse.error(e.getMessage());
        }
        return XhrResponse.success();
    }

    @RequestMapping("student/getStudentsByClazzId")
    public String getStudentsByClazzId(Long clazzId, Model model) {
        List<Student> studentList = studentService.getStudentsByClazzId(clazzId);
        Clazz clazz = clazzService.getById(clazzId);
        model.addAttribute("studentList", studentList);
        model.addAttribute("clazz", clazz);
        return "student_list_byClazzId";
    }

}
